﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RendicionGastos.Models;
using RendicionGastos.CentroCostoService;

namespace RendicionGastos.Controllers
{
        [Authorize(Roles = "Administrador")]
    public class CentroCostoController : Controller
    {

        public ActionResult Index()
        {

            List<CentroCostoModel> ccmList = new List<CentroCostoModel>();

            ConfigCentroCostoClient client = new ConfigCentroCostoClient();

            var ccsWS = new RendicionGastosEntity.CentroCosto[0];
            ccsWS = client.listarCentroCosto();

            
            foreach (var item in ccsWS)
            {
                var cc = new CentroCostoModel();
                cc.codigo = item.codigo;
                cc.descripcion = item.descripcion;
                ccmList.Add(cc);
            } 

            return View(ccmList);
        }

        public ActionResult crear()
        {
            return View();
        }

        [HttpPost]
        public ActionResult crear(int codigo, string descripcion)
        {
            ConfigCentroCostoClient client = new ConfigCentroCostoClient();
            client.agregarCentroCosto(codigo, descripcion);
            return View();
        }

        public ActionResult eliminar(int codigo)
        {
            ConfigCentroCostoClient client = new ConfigCentroCostoClient();
            var obj = client.obtenerCentroCosto(codigo);

            var cc = new CentroCostoModel();
            cc.codigo = obj.codigo;
            cc.descripcion = obj.descripcion;

            return View(cc);
        }

        [HttpPost]
        public ActionResult eliminar(int codigo, string descripcion)
        {
            ConfigCentroCostoClient client = new ConfigCentroCostoClient();
            var obj = new RendicionGastosEntity.CentroCosto();
            obj.codigo = codigo;
            obj.descripcion = descripcion;
            client.eliminarCentroCosto(obj);
            return View();
        }

    }
}
